package ink.lovejinhu.leetcode;

/**
 * @Author: jin
 * @Date: 2021/9/10 9:03
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Solution_0888 {

    public int[] preSum(int[] num) {
        int[] ints = new int[num.length];
        int sum = 0;
        for (int i = 0; i < num.length; i++) {
            ints[i] = sum += num[i];
        }
        return ints;
    }

    public int sum(int[] num) {

        int sum = 0;
        for (int i = 0; i < num.length; i++) {
            sum += num[i];
        }
        return sum;
    }

    /**
     * 叫唤一根糖果后，使得两个人的糖果总数一样
     * 能相等意味着
     * 假设x，y
     * x-a+b=y-b+a
     * x-y=a-b+a-b
     * x-y=2(a-b)
     * y-x=
     *
     * @param aliceSizes
     * @param bobSizes
     * @return
     */
    public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
        int[] ints = new int[2];
//        int[] aliceSizesSum = preSum(aliceSizes);
//        int[] bobSizesSum = preSum(bobSizes);
        int aliceSum = this.sum(aliceSizes);
        int bobSum = this.sum(bobSizes);
        for (int i = 0; i < aliceSizes.length; i++) {
            for (int j = 0; j < bobSizes.length; j++) {
                int aSum = aliceSum - aliceSizes[i] + bobSizes[j];
                int bSum = bobSum - bobSizes[j] + aliceSizes[i];
                if (aSum == bSum) {
                    ints[0] = aliceSizes[i];
                    ints[1] = bobSizes[j];
                    break;
                }
            }
        }
        return ints;
    }


}
